﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Data.Entity;
using JustMVCTest.Models;
using Telerik.Web.Mvc;
using System.Text;
using System.IO;
using System.Collections;
using Telerik.Web.Mvc.Extensions;
using System.Diagnostics;

namespace JustMVCTest.Controllers
{
    public class POsListController : Controller
    {
        private PODBContext db = new PODBContext();
        //
        // GET: /POsList/

        public ActionResult POsExportTableToCSV(string Description_Field, string VendorName_Field, string Delivery_Date_Field, string Status_Field,
                string Requisitioner_Field,
                string DeliveryDate_Req_Field,
                string Cost_Cent_Field,
                string Int_Order_Field,
                string Prof_Cent_Field,
                string Percentage_Field,
                string cost_elem_Field,
                string co_el_name_Field,
                string Vendor_Field,
                string UM_Field,
                string T_Curr_Field,
                string Sch_Lin_Field,
                string Ord_Qty_Field,
                string Open_Qty_Field,
                string Open_Amt_T_Field,
                string Order_Amt_Field,
                string Open_Amt_L_Field,
                string TSSC_Pending_Send_Date_Field,
                string Purchase_O_Field,
                string isfirstload)
        {
            if(string.IsNullOrEmpty(isfirstload))
            {
                Description_Field = "1";
                VendorName_Field = "1";
                Delivery_Date_Field = "1";
                Status_Field = "1";
                Requisitioner_Field = "1";
                DeliveryDate_Req_Field = "1";
                Cost_Cent_Field = "1";
                Int_Order_Field = "1";
                Prof_Cent_Field = "1";
                Percentage_Field = "1";
                cost_elem_Field = "1";
                co_el_name_Field = "1";
                Vendor_Field = "1";
                UM_Field = "1";
                T_Curr_Field = "1";
                Sch_Lin_Field = "1";
                Ord_Qty_Field = "1";
                Open_Qty_Field = "1";
                Open_Amt_T_Field = "1";
                Order_Amt_Field = "1";
                Open_Amt_L_Field = "1";
                TSSC_Pending_Send_Date_Field = "1";
                Purchase_O_Field = "1";
            }

            MemoryStream output = new MemoryStream();
            StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
            writer.Write("po_number,");
            writer.Write("item,");
            if (!string.IsNullOrEmpty(Status_Field))
            writer.Write("status,");

            if (!string.IsNullOrEmpty(Description_Field))
            writer.Write("description,");

            if (!string.IsNullOrEmpty(Delivery_Date_Field))
            writer.Write("delivery_date,");

            if (!string.IsNullOrEmpty(Requisitioner_Field))
            writer.Write("requisitioner,");

            if (!string.IsNullOrEmpty(DeliveryDate_Req_Field))
            writer.Write("delivery_date_request,");

            if (!string.IsNullOrEmpty(Cost_Cent_Field))
            writer.Write("cost_center,");

            if (!string.IsNullOrEmpty(Int_Order_Field))
            writer.Write("internal_order,");

            if (!string.IsNullOrEmpty(Prof_Cent_Field))
            writer.Write("prof_center,");

            if (!string.IsNullOrEmpty(Percentage_Field))
            writer.Write("percentage,");

            if (!string.IsNullOrEmpty(cost_elem_Field))
            writer.Write("cost_element,");

            if (!string.IsNullOrEmpty(co_el_name_Field))
            writer.Write("cost_element_name,");

            if (!string.IsNullOrEmpty(Vendor_Field))
            writer.Write("vendor,");

            if (!string.IsNullOrEmpty(VendorName_Field))
            writer.Write("vendor_name,");

            if (!string.IsNullOrEmpty(UM_Field))
            writer.Write("um,");

            if (!string.IsNullOrEmpty(T_Curr_Field))
            writer.Write("T_currency,");

            if (!string.IsNullOrEmpty(Sch_Lin_Field))
            writer.Write("sch_lin,");

            if (!string.IsNullOrEmpty(Ord_Qty_Field))
            writer.Write("order_quantity,");

            if (!string.IsNullOrEmpty(Open_Qty_Field))
            writer.Write("open_quantity,");

            if (!string.IsNullOrEmpty(Open_Amt_T_Field))
            writer.Write("open_amount_T,");

            if (!string.IsNullOrEmpty(Order_Amt_Field))
            writer.Write("order_amount,");

            if (!string.IsNullOrEmpty(Open_Amt_L_Field))
            {
                writer.Write("open_amount_L,");   
            }
            if (!string.IsNullOrEmpty(TSSC_Pending_Send_Date_Field))
            {
                writer.Write("TSSC_Pending_Send_Date,");
            }
            if (!string.IsNullOrEmpty(Purchase_O_Field))
            {
                writer.Write("Purchase_O");
            }
            writer.WriteLine();

            var user_id = User.Identity.Name;
            user_id = user_id.Replace("\\", ",");
            var words = user_id.Split(new Char[] { ',' });
            user_id = words[1];

            var pos_list = from poes in db.POs
                           where (poes.latest == 1)
                           select poes;

            var mail_add = from users in db.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           select users.email;
            var users_list = from users in db.Papi_Users_tbl
                             where (users.domain_username == user_id)
                             select users;

            var is_superuser = false;

            if (users_list.Count() > 0)
            {
                is_superuser = users_list.First().is_superuser;
            }

            var cost_cent = from users in db.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Cost_Cent;
            string[] cost_centArray = cost_cent.ToArray();
            String scost_cent = cost_centArray[0];

            var int_Order = from users in db.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Int_Order;

            string[] int_OrderArray = int_Order.ToArray();
            String sint_Order = int_OrderArray[0];

            var requisitioner = from users in db.Papi_Users_tbl
                                where (users.domain_username == user_id)
                                select users.Requisitio;

            string[] requisitionerArray = requisitioner.ToArray();
            String sRequisitioner = requisitionerArray[0];

            IQueryable<PO> pos_req = null;
            IQueryable<PO> pos_cc = null;
            IQueryable<PO> pos_io = null;
            IQueryable<PO> pos = null;

            if (!is_superuser)
            {
                pos_req = (from users in db.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           join poes in pos_list
                           on users.Requisitio equals poes.Requisitioner
                           select poes);

                pos_cc = (from users in db.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Cost_Cent equals poes.Cost_Cent
                          select poes);
                pos_io = (from users in db.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Int_Order equals poes.Int_Order
                          select poes);
                pos = (from poes in pos_req select poes)
                      .Union
                      (from poes1 in pos_cc select poes1)
                      .Union
                      (from poes2 in pos_io select poes2);
            }

            else
            {
                pos = pos_list;
            }

            foreach (PO po in pos)
            {
                if ((po.Status != 99) && (po.latest == 1))
                {
                    writer.Write(po.PONumber); writer.Write(",");
                    writer.Write(po.Item); writer.Write(",");
                    if (!string.IsNullOrEmpty(Status_Field))
                    {
                        writer.Write(po.Status); writer.Write(",");
                    }

                    if (!string.IsNullOrEmpty(Description_Field))
                    {
                        String Descr_tmp = po.Descr.Replace(",", " ");
                        writer.Write(Descr_tmp); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Delivery_Date_Field))
                    {
                        writer.Write(po.DeliveryDate); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Requisitioner_Field))
                    {
                        writer.Write(po.Requisitioner); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(DeliveryDate_Req_Field))
                    {
                        writer.Write(po.DeliveryDate_Req); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Cost_Cent_Field))
                    {
                        writer.Write(po.Cost_Cent); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Int_Order_Field))
                    {
                        writer.Write(po.Int_Order); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Prof_Cent_Field))
                    {
                        writer.Write(po.Prof_Cent); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Percentage_Field))
                    {
                        writer.Write(po.Percentage); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(cost_elem_Field))
                    {
                        writer.Write(po.cost_elem); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(co_el_name_Field))
                    {
                        writer.Write(po.co_el_name); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Vendor_Field))
                    {
                        writer.Write(po.Vendor); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(VendorName_Field))
                    {
                        String VendorName_tmp = po.Descr.Replace(",", " ");
                        writer.Write(VendorName_tmp); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(UM_Field))
                    {
                        writer.Write(po.UM); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(T_Curr_Field))
                    {
                        writer.Write(po.T_Curr); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Sch_Lin_Field))
                    {
                        writer.Write(po.Sch_Lin); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Ord_Qty_Field))
                    {
                        writer.Write(po.Ord_Qty); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Open_Qty_Field))
                    {
                        writer.Write(po.Open_Qty); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Open_Amt_T_Field))
                    {
                        writer.Write(po.Open_Amt_T); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Order_Amt_Field))
                    {
                        writer.Write(po.Order_Amt); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Open_Amt_L_Field))
                    {
                        writer.Write(po.Open_Amt_L); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(TSSC_Pending_Send_Date_Field))
                    {
                        writer.Write(po.TSSC_Pending_Send_Date); writer.Write(",");
                    }
                    if (!string.IsNullOrEmpty(Purchase_O_Field))
                    {
                        writer.Write(po.Purchase_O);
                    }
                    writer.WriteLine();
                }
            }
            writer.Flush();
            output.Position = 0;

            return File(output, "text/comma-separated-values", "Open_POs.csv");
        }

        public ViewResult Index(string Description_Field, string VendorName_Field, string Delivery_Date_Field, string Status_Field,
                string Requisitioner_Field,
                string DeliveryDate_Req_Field,
                string Cost_Cent_Field,
                string Int_Order_Field,
                string Prof_Cent_Field,
                string Percentage_Field,
                string cost_elem_Field,
                string co_el_name_Field,
                string Vendor_Field,
                string UM_Field,
                string T_Curr_Field,
                string Sch_Lin_Field,
                string Ord_Qty_Field,
                string Open_Qty_Field,
                string Open_Amt_T_Field,
                string Order_Amt_Field,
                string Open_Amt_L_Field,
                string TSSC_Pending_Send_Date_Field,
                string Purchase_O_Field,
                string isfirstload)
        {

            var user_id = User.Identity.Name;
            user_id = user_id.Replace("\\", ",");
            var words = user_id.Split(new Char[] { ',' });
            user_id = words[1];
            
            var pos_list = from poes in db.POs
                           where (poes.latest == 1 && poes.Status != 99)
                           select poes;

             var users_list = from users in db.Papi_Users_tbl
                             where (users.domain_username == user_id)
                             select users;
            
            var is_superuser = false;

            if (users_list.Count() > 0) 
           {
               is_superuser = users_list.First().is_superuser;
           }

            var mail_add = from users in db.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           select users.email;

            var cost_cent = from users in db.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Cost_Cent;

            string[] cost_centArray = cost_cent.ToArray();

            var int_Order = from users in db.Papi_Users_tbl
                            where (users.domain_username == user_id)
                            select users.Int_Order;
                            
            string[] int_OrderArray = int_Order.ToArray();

            String scost_cent = cost_centArray[0];
            String sint_Order = int_OrderArray[0];

            var requisitioner = from users in db.Papi_Users_tbl
                                where (users.domain_username == user_id)
                                select users.Requisitio;

            string[] requisitionerArray = requisitioner.ToArray();
            String sRequisitioner = requisitionerArray[0];

            IQueryable<PO> pos_req = null;
            IQueryable<PO> pos_cc = null;
            IQueryable<PO> pos_io = null;
            IQueryable<PO> pos = null;

            if (!is_superuser)
            {
                pos_req = (from users in db.Papi_Users_tbl
                           where (users.domain_username == user_id)
                           join poes in pos_list
                           on users.Requisitio equals poes.Requisitioner
                           select poes);

                pos_cc = (from users in db.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Cost_Cent equals poes.Cost_Cent
                          select poes);
                pos_io = (from users in db.Papi_Users_tbl
                          where (users.domain_username == user_id)
                          join poes in pos_list
                          on users.Int_Order equals poes.Int_Order
                          select poes);
                pos = (from poes in pos_req select poes)
                      .Union
                      (from poes1 in pos_cc select poes1)
                      .Union
                      (from poes2 in pos_io select poes2);
            }

            else
            {
                pos = pos_list;
            }


         /*   if (scost_cent != null && sint_Order != null)
            {
                pos = pos.Where(p => ((p.Cost_Cent == scost_cent) || (p.Int_Order == sint_Order)));
            }
            else if (scost_cent != null)
            {
                pos = pos.Where(p => (p.Cost_Cent == scost_cent));
            }
            else if (sint_Order != null)
            {
                pos = pos.Where(p => (p.Int_Order == sint_Order));
            }
            else
            {
                pos = pos.Where(p => (p.Requisitioner == sRequisitioner));
            }

            // pos = pos.Where(s => s.latest == 1);
            pos = pos.Where(s => (s.Status == 1) || (s.Status == 0) || (s.Status == 2)); */

                ViewData["Description_Field"] = true;
                ViewData["VendorName_Field"] = true;
                ViewData["Delivery_Date_Field"] = true;
                ViewData["Status_Field"] = true;
                ViewData["Requisitioner_Field"] = true;
                ViewData["DeliveryDate_Req_Field"] = true;
                ViewData["Cost_Cent_Field"] = true;
                ViewData["Int_Order_Field"] = true;
                ViewData["Prof_Cent_Field"] = true;
                ViewData["Percentage_Field"] = true;
                ViewData["cost_elem_Field"] = true;
                ViewData["co_el_name_Field"] = true;
                ViewData["Vendor_Field"] = true;
                ViewData["UM_Field"] = true;
                ViewData["T_Curr_Field"] = true;
                ViewData["Sch_Lin_Field"] = true;
                ViewData["Ord_Qty_Field"] = true;
                ViewData["Open_Qty_Field"] = true;
                ViewData["Open_Amt_T_Field"] = true;
                ViewData["Order_Amt_Field"] = true;
                ViewData["Open_Amt_L_Field"] = true;
                ViewData["TSSC_Pending_Send_Date_Field"] = true;
                ViewData["Purchase_O_Field"] = true;

                Session["Came_From"] = "POsList";

                if (!string.IsNullOrEmpty(isfirstload))
                {
                    if (string.IsNullOrEmpty(Description_Field))
                    {
                      
                        ViewData["Description_Field"] = false;
                    }

                    if (string.IsNullOrEmpty(VendorName_Field))
                    {
                        ViewData["VendorName_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Delivery_Date_Field))
                    {
                        ViewData["Delivery_Date_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Status_Field))
                    {
                        ViewData["Status_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Requisitioner_Field))
                    {
                        ViewData["Requisitioner_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(DeliveryDate_Req_Field))
                    {
                        ViewData["DeliveryDate_Req_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Cost_Cent_Field))
                    {
                        ViewData["Cost_Cent_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Int_Order_Field))
                    {
                        ViewData["Int_Order_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Prof_Cent_Field))
                    {
                        ViewData["Prof_Cent_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Percentage_Field))
                    {
                        ViewData["Percentage_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(cost_elem_Field))
                    {
                        ViewData["cost_elem_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(co_el_name_Field))
                    {
                        ViewData["co_el_name_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Vendor_Field))
                    {
                        ViewData["Vendor_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(UM_Field))
                    {
                        ViewData["UM_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(T_Curr_Field))
                    {
                        ViewData["T_Curr_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Sch_Lin_Field))
                    {
                        ViewData["Sch_Lin_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Ord_Qty_Field))
                    {
                        ViewData["Ord_Qty_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Open_Qty_Field))
                    {
                        ViewData["Open_Qty_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Open_Amt_T_Field))
                    {
                        ViewData["Open_Amt_T_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Order_Amt_Field))
                    {
                        ViewData["Order_Amt_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Open_Amt_L_Field))
                    {
                        ViewData["Open_Amt_L_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(TSSC_Pending_Send_Date_Field))
                    {
                        ViewData["TSSC_Pending_Send_Date_Field"] = false;
                    }
                    if (string.IsNullOrEmpty(Purchase_O_Field))
                    {
                        ViewData["Purchase_O_Field"] = false;
                    }
                }
            // lalalalalala

            if (is_superuser) {
             return View(pos_list);    //returns ALL open po's
            }

            return View(pos);
        }

      
    }
}